package managedbean.Product;

import entity.Discount;
import entity.Product;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.ProductBeanLocal;

@ManagedBean
@ViewScoped
public class EditProductManagerBean implements Serializable
{
    @EJB
    private ProductBeanLocal productBeanLocal;
    private Product productToEdit;
    private String productType;
    private String statusMessage;

    public Product getProductToEdit() {
        return productToEdit;
    }
    public void setProductToEdit(Product productToEdit) {
        this.productToEdit = productToEdit;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public EditProductManagerBean() {}
    
    public void initView(PhaseEvent event){
        if(productToEdit == null)
        {            
            productToEdit = (Product)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedProduct"); 
            productType = productToEdit.getProductType(); 
        }
    }
    
    public void updateProduct(ActionEvent event){
        productBeanLocal.updateProduct(productType, productToEdit.getProductPrice());
        System.out.println("ProductType after edit:"+ productToEdit.getProductType());
        
        statusMessage = "Product updated Successfully"; 
 
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Update Product Result: " + statusMessage, "")); 
    }    
}

